//
//  SpecialViewController.m
//  demo
//
//  Created by dllo on 15/7/11.
//  Copyright (c) 2015年 dllo. All rights reserved.
//

#import "SpecialViewController.h"

static int page = 1;
@interface SpecialViewController ()<UITableViewDataSource, UITableViewDelegate>
@end

@implementation SpecialViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    self.view.backgroundColor =[UIColor whiteColor];
    self.navigationItem.title = @"专题";
    self.dataArray = [NSMutableArray array];
    [self createSpecialTableView];
    [self getData:page where:nil];
    [self exampleUpPush];
    [self exampleDownPush];
    //[self loadingGet];
}

-(void)createSpecialTableView
{
    self.specialTableView = [[UITableView alloc]initWithFrame:self.view.bounds style:UITableViewStylePlain];
    self.specialTableView.delegate = self;
    self.specialTableView.dataSource = self;
    [self.view addSubview:self.specialTableView];
    [_specialTableView release];
    //self.specialTableView.contentInset = UIEdgeInsetsMake(5, 0, 70, 0);
    self.specialTableView.contentInset = UIEdgeInsetsMake(10, 0, 70, 0);
    //去掉cell中间的线
    self.specialTableView.separatorStyle = UITableViewCellSeparatorStyleNone;
}

//从详情页面返回后 显示navgation和tabBar
- (void)viewWillAppear:(BOOL)animated
{
    self.tabBarController.tabBar.hidden = NO;
    self.navigationController.navigationBarHidden = NO;
    [self.navigationController.navigationBar setBackgroundImage:[UIImage imageNamed:@"title.jpg"] forBarMetrics:UIBarMetricsDefault];
}


//第一次获取数据
-(void)getData:(int)page where:(NSString *)where
{
    NSString *str =[NSString stringWithFormat:@"http://chanyouji.com/api/articles.json?page=%d", page];
    [AFNetworkHandler GetWithAFNByURL:str completion:^(id result) {
        NSArray *tempArr = result;
        NSMutableArray *temp = [NSMutableArray array];
        temp = [special arrayWithModelByArray:tempArr];
        if ([where isEqualToString:@"upPush"] == YES||[where isEqualToString:nil] == YES) {
            for (special *s in temp) {
                [self.dataArray addObject:s];
            }
            [self.specialTableView reloadData];
            // 拿到当前的上拉刷新控件，结束刷新状态
            [self.specialTableView.footer endRefreshing];
        }else if ([where isEqualToString:@"downPush"] == YES){
            self.dataArray = [NSMutableArray array];
            for (NSInteger i = temp.count - 1; i >= 0; i--) {
                special *s = temp[i];
                [self.dataArray insertObject:s atIndex:0];
            }
            [self.specialTableView reloadData];
            // 拿到当前的下拉刷新控件，结束刷新状态
            [self.specialTableView.header endRefreshing];
        }
    }];
}


//上拉刷新
-(void)loadMoreData
{   //向上推动时page自加
    NSString *where = @"upPush";
    page++;
    [self getData:page where: where];
}


-(void)exampleDownPush
{
    // 设置回调（一旦进入刷新状态，就调用target的action，也就是调用self的loadMoreData方法）
    self.specialTableView.footer = [MJRefreshBackNormalFooter footerWithRefreshingTarget:self refreshingAction:@selector(loadMoreData)];
}


#pragma - mark 下拉刷新
-(void)loadNewData
{
    NSString *where = @"downPush";
    static int a = 1;
    [self getData:a where:where];
}

-(void)exampleUpPush
{
    self.specialTableView.header = [MJRefreshNormalHeader headerWithRefreshingBlock:^{
        [self loadNewData];
    }];
    //马上进入刷新状态
    [self.specialTableView.header beginRefreshing];
}


-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    SpecialTableViewCell *cell = [SpecialTableViewCell tableView:tableView indexPath:indexPath];
    [cell.back setImage:[UIImage imageNamed:@"ArticleCoverMask"]];
    cell.special = self.dataArray[indexPath.row];
    return cell;
}


-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    SpecialDetailViewController *SpecialDetailVC = [[SpecialDetailViewController alloc]init];
    SpecialDetailVC.number = [self.dataArray[indexPath.row] specialId].integerValue;
    [self.navigationController pushViewController:SpecialDetailVC animated:YES];
}

-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    return self.dataArray.count;
}

-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    return (self.view.frame.size.height - 49 - 64 - 40 - 10) / 2;
}
- (void)scrollViewDidScroll:(UIScrollView *)scrollView
{
    NSArray *visibleCells = [self.specialTableView visibleCells];
    for (SpecialTableViewCell *cell in visibleCells) {
        [cell cellOnTableView:self.specialTableView didScrollOnView:self.view];
    }
}
#pragma mark - 开始拖拽
- (void)scrollViewWillBeginDragging:(UIScrollView *)scrollView
{
    self.tabBarController.tabBar.hidden = YES;
}
#pragma mark - 结束减速
- (void)scrollViewDidEndDecelerating:(UIScrollView *)scrollView
{
    self.tabBarController.tabBar.hidden = NO;
}
- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
 #pragma mark - Navigation
 
 // In a storyboard-based application, you will often want to do a little preparation before navigation
 - (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
 // Get the new view controller using [segue destinationViewController].
 // Pass the selected object to the new view controller.
 }
 */

@end
